package com.c.ctgproject.http

import android.content.Context
import com.c.ctgproject.utils.CustomToast
import com.c.ctgproject.wiget.dialog.LoadingDialog

import io.reactivex.Observer
import io.reactivex.disposables.Disposable
import okhttp3.ResponseBody

/**
 * 创建时间:2018/12/4
 * 作者:LiBW
 * 描述:通用Observer需要自己对代码进行try catch处理
 */
open class MyObserver(private val mContext: Context, private val listener: ObserverListener, private val showDialog: Boolean = true) : Observer<ResponseBody> {
    private val dialog: LoadingDialog by lazy { LoadingDialog(mContext) }

    //加载完成 进度条消失
    override fun onComplete() {
        dialog.dismiss()
    }

    //加载开始 展示进度条
    override fun onSubscribe(d: Disposable) {
        if (showDialog) {
            dialog.show()
        }
    }

    //加载完成回调
    override fun onNext(t: ResponseBody) {
        dialog.dismiss()
        listener.onNext(t)
    }

    //出现错误 进度条消失 按需求展示错误页面
    override fun onError(e: Throwable) {
        dialog.dismiss()
        CustomToast.showToast(mContext, e.message.toString())
    }
}
